package com.start.pay.core;

import com.start.common.api.Result;
import com.start.common.tools.IPTools;
import com.start.common.tools.SpringContextTools;
import com.start.common.tools.StartEnumTools;
import com.start.pay.core.entity.StartPayOrderEntity;
import com.start.pay.core.enums.StartPayState;
import com.start.pay.core.service.StartPayOrderService;
import com.start.security.annotations.IgnoreAuth;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Tag(name = "支付平台订单核心API")
@RequestMapping("start/pay/orderApi")
public class StartPayOrderApiController {

    @Autowired
    private StartPayOrderService startPayOrderService;

   @Operation(summary = "创建订单")
    @PostMapping("/createOrder/{platformType}/{appId}")
    public Result<?> createOrder(@PathVariable("platformType") String platformType, @PathVariable("appId") String appId, @RequestBody StartPayOrderEntity entity) {
        entity.setClientIp(IPTools.getIpAddr(SpringContextTools.getHttpServletRequest()));
        return Result.ok(StartPayServerService.getInstance().createOrder(platformType, appId, entity));
    }

   @Operation(summary = "创建支付")
    @GetMapping("/createPay/{orderNo}")
    public Result<?> createPay(@PathVariable("orderNo") String orderNo) {
        return Result.ok(StartPayServerService.getInstance().createPay(orderNo));
    }

   @Operation(summary = "根据订单号查询订单")
    @GetMapping("/queryOrder/{orderNo}")
    public Result<?> queryOrder(@PathVariable("orderNo") String orderNo) {
        return Result.ok(StartPayServerService.getInstance().queryOrder(orderNo));
    }

   @Operation(summary = "根据订单号查询订单支付状态")
    @GetMapping("/queryOrderPayState/{orderNo}")
    public Result<?> queryOrderPayState(@PathVariable("orderNo") String orderNo) {
        return Result.ok(StartPayServerService.getInstance().queryOrderPayState(orderNo));
    }

   @Operation(summary = "根据订单号同步第三方平台订单")
    @GetMapping("/checkOrder/{orderNo}")
    public Result<?> checkOrder(@PathVariable("orderNo") String orderNo) {
        return Result.ok(StartOrderCheckServer.getInstance().checkOrder(orderNo));
    }

   @Operation(summary = "根据订单号取消订单")
    @GetMapping("/cancelOrder/{orderNo}")
    public Result<?> cancelOrder(@PathVariable("orderNo") String orderNo) {
        return Result.ok(StartPayServerService.getInstance().cancelOrder(orderNo));
    }


    @IgnoreAuth
   @Operation(summary = "获取全部支付状态")
    @GetMapping("/getAllPayState")
    public Result<?> getAllPayState() {
        return Result.ok(StartEnumTools.getEnumDic(StartPayState.class));
    }

    @GetMapping("/getAllPlatformType")
    public Result<?> getAllPlatformType() {
        return Result.ok(StartPayPlatformManager.getInstance().getAllPayPlatformType());
    }
}
